﻿// COPYRIGHT 2012, Justin Michalczak
//
// This file is licenced to the public under the Apache
// License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CopperVineSoftware.Stackr.Parser.Lexemes
{
    /// <summary>Definition of a constant integer value.</summary>
    public class IntegerConstantDefinition : Definition
    {
        private readonly string name;
        private readonly ushort value;

        /// <summary>The name to bind the integer constant value to.</summary>
        public string Name
        {
            get
            {
                return this.name;
            }
        }

        /// <summary>The defined integer constant value.</summary>
        public ushort Value
        {
            get
            {
                return this.value;
            }
        }

        /// <summary>Constructor.</summary>
        /// <param name="name">Name to bind the integer value to.</param>
        /// <param name="value">Integer value to be bound.</param>
        /// <exception cref="ArgumentNullException">If the <paramref name="name"/> parameter is <c>null</c> or empty.</exception>
        public IntegerConstantDefinition(string name, ushort value)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");

            this.name = name;
            this.value = value;
        }

        /// <summary>Determines whether two integer constant definitions are equal.</summary>
        /// <remarks>
        ///     Two integer constant definitions are considered equal if and only if
        ///     they are bound to the same name and they define the same integer value.
        /// </remarks>
        /// <param name="obj">Object to test for equality.</param>
        /// <returns>True if the <paramref name="obj"/> parameter is equal to this integer constant definition.</returns>
        public override bool Equals(object obj)
        {
            if (!(obj is IntegerConstantDefinition))
                return false;

            IntegerConstantDefinition rhs = obj as IntegerConstantDefinition;
            return (this.name == rhs.name && this.value.Equals(rhs.value));
        }

        /// <summary>Retrieves a hash value for this integer constant definition.</summary>
        /// <returns>A hash code for this integer constant definition.</returns>
        public override int GetHashCode()
        {
            return (this.name.GetHashCode() ^ this.value.GetHashCode());
        }

        /// <summary>Retrieves a string representation of this integer constant definition.</summary>
        /// <returns>A string represenation of this integer constant definition.</returns>
        public override string ToString()
        {
            return string.Format("{0}: {1}", this.name, this.value);
        }
    }
}
